#!/usr/bin/env python

#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""Index page for OAIS. The start page for searching.

Outputs the index page for the OAIS web tool.
"""

__version__ = "1.0"

# imports
from dbinter import oai_db
from util import *
from oaicookie import *
import cgi
from directory import *

class index:
	"""
	The index class handles the output of the index page.
	"""
	def __init__(self):
		"""
		Returns: None

		Initializes the page. Connects to the database and retrieves the total number
		of records available for search. Reads in the config file for variables.
		"""
		# connect to the database
		self.db = oai_db()
		try:
			self.db.connect()
			# get total record number
			result = self.db.query( "SELECT count(*) FROM harvestSearch;" )
			self.rec_count = 'Searching ' + commas(result[1][0][0]) + ' records.'
			#self.rec_count = ''
			# disconnect from the db
			self.db.disconnect()
		except:
			# db is unavailable
			self.rec_count = 'Database unavailable. Some search features may not work.'

	def process(self):
		"""
		Returns: None

		Print out all parts of the page. If a 'message' file is available, append
		its contents to the page.
		"""
		message = ''
		msglines = []
		try:
			msgfile = open('message','r')
			msglines = msgfile.readlines()
			msgfile.close()
		except:
			pass

		if len(msglines) > 0:
			message = '<br>'.join(msglines)

		# pull cgi
		params = cgi.FieldStorage()
		directory = '0'
		viewparent = '0'
		if params.has_key('d'):	directory = cui(params['d'].value)
		if params.has_key('vp'): viewparent = cui(params['vp'].value)

		# make the page

		# print header
		outbit('pg_header_1')

		outbit('pg_title_1')

		outbit('pg_bar_short')

		outbit('pg_tips')

		# if you're not browsing the directory
		if len(directory) < 32:
			# print the search box
			outbit('ie_warn')

			outbit('pg_search_main')

		# output directory if desired
		if len(directory) == 32:
			# print sub directory
			printDirectory(directory,0,viewparent)
		elif directory == '1':
			# print root directory
			printDirectory()
		else:
			# no directory
			outbit('pg_directory_link')

		data = (self.rec_count, message)
		outbit('pg_footer_1',data)


# begin execution
def index_main():
	"""
	Returns: None
	
	Main function. This is called upon execution.
	"""
	print "Content-type: text/html\n\n"
	page = index()
	data = page.process()

if __name__ == '__main__' or __name__ == 'index':
	run = index_main()

#eof

